在firebase製作登入系統
可以使用myRef.child("member").get().addOnCompleteListener進入資料庫裡
task.getResult().getChildrenCount()可以偵測有幾筆資料
使用
myRef.child("member").get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
Boolean ok = true;
long a =task.getResult().getChildrenCount();
if(account.getText().toString().matches("") || password.getText().toString().matches("")) {
Toast.makeText(login.this, "請輸入帳號密碼", Toast.LENGTH_SHORT).show();
}
else {
for (int i = 1; i <= a; i++) {
if (account.getText().toString().equals(task.getResult().child(String.valueOf(i)).child("account").getValue()) & password.getText().toString().equals(task.getResult().child(String.valueOf(i)).child("password").getValue())) {
ok = false;
SharedPreferences member_pref = getSharedPreferences("member", MODE_PRIVATE);
member_pref.edit().putString("USER", account.getText().toString())
.commit();
Intent intent = new Intent(login.this, MainActivity.class);
startActivity(intent);
}
}
if (ok){
Toast.makeText(login.this,"帳密錯誤",Toast.LENGTH_SHORT).show();
}
}
}
});